package org.rzp.ideaplug.statistic.push;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.rzp.ideaplug.statistic.R;
import org.rzp.ideaplug.statistic.statistic.StatisticInfo;

import java.io.IOException;
import java.nio.charset.Charset;

public class PushStatisticUtil {


    public static void pushInfoNotify(String user, String url, String projectPath, StatisticInfo statistic, Project project) {
        PushResponse pushResponse = pushInfo(user, url, projectPath, statistic);
        notify(pushResponse, projectPath, project);
    }

    public static void pushInfoOnly(String user, String url, String projectPath, StatisticInfo statistic) {
        PushResponse pushResponse = pushInfo(user, url, projectPath, statistic);
    }


    private static void notify(PushResponse pushResponse, String path, Project project) {
        if (pushResponse.isSuccess()) {
            Notification notification = new Notification(R.GROUP_DISPLAY_ID, "[" + path + "] statistic push success", JSON.toJSONString(pushResponse), NotificationType.INFORMATION);
            notification.notify(project);
        } else {
            Notification notification = new Notification(R.GROUP_DISPLAY_ID, "[" + path + "] statistic push error", JSON.toJSONString(pushResponse), NotificationType.ERROR);
            notification.notify(project);
        }
    }


    private static PushResponse pushInfo(String user, String url, String project, StatisticInfo statistic) {
        PushResponse pushResponse = new PushResponse();
        try {
            OkHttpClient client = new OkHttpClient();
            Request.Builder requestBuilder = new Request.Builder().url(url);
            FormBody.Builder bodyBuilder = new FormBody.Builder(Charset.forName("UTF-8"));
            bodyBuilder.add("user", user);
            bodyBuilder.add("project", project);
            bodyBuilder.add("totalLine", String.valueOf(statistic.getTotalLine()));
            bodyBuilder.add("sourceCodeLine", String.valueOf(statistic.getSourceCodeLine()));
            bodyBuilder.add("commentLine", String.valueOf(statistic.getCommentLine()));
            bodyBuilder.add("blankLine", String.valueOf(statistic.getBlankLine()));
            bodyBuilder.add("annotationLine", String.valueOf(statistic.getAnnotationLine()));
            FormBody formBody = bodyBuilder.build();

            Request postRequest = requestBuilder.method("POST", formBody).build();

            Response response = client.newCall(postRequest).execute();
            int code = response.code();
            String string = response.body().string();
            pushResponse.setCode(code);
            pushResponse.setMessage(string);
            if (code == 200) {
                JSONObject jsonObject = JSON.parseObject(string);
                int rCode = jsonObject.getIntValue("code");
                pushResponse.setCode(rCode);
                pushResponse.setSuccess(rCode == 200);
                pushResponse.setMessage(jsonObject.getString("message"));
            } else {
                pushResponse.setSuccess(false);
            }
        } catch (IOException e) {
            e.printStackTrace();
            pushResponse.setCode(-500);
            pushResponse.setMessage(e.getMessage());
        }
        return pushResponse;
    }


}
